package com.un.ebs.finance.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.finance.AccountTypeEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @email liubocs@163.com
 * @date 2020/9/7 20:05
 */
@Data
public class CertificateDto implements Serializable {

    /**
     * 凭证ID
     */
    @JSONField(name = "ID")
    private String id;

    /**
     * 账簿编号
     */
    @JSONField(name = "FAccountBookID")
    private FNumberDto accountBookId;

    /**
     * 凭证字
     */
    @JSONField(name = "FVOUCHERGROUPID")
    private FNumberDto voucherGroupId;

    /**
     * 附件数
     */
    @JSONField(name = "FATTACHMENTS")
    private String attachments;

    /**
     * 凭证日期
     */
    @JSONField(name = "FDate", format = "yyyy-MM-dd HH:mm:ss")
    private Date date;

    /**
     * 凭证内容
     */
    @JSONField(name = "FEntity")
    private List<CertificateEntityDto> entity;

    @JSONField(name = "Examine")
    private String examineStatus;

    public void addDetail(String accountId, String explanation, double money, AccountTypeEnum type, FDetailDto dim) {
        if (StringUtils.isNullOrEmpty(entity)) {
            entity = new ArrayList<>();
        }

        CertificateEntityDto obj = new CertificateEntityDto();
        obj.setAccountId(FNumberDto.value(accountId));
        obj.setExplanation(explanation);
        obj.setAmountFor(String.valueOf(money));
        obj.setCurrencyId(FNumberDto.value("PRE001"));
        obj.setExchangeRateType(FNumberDto.value("HLTX01_SYS"));
        obj.setExchangeRate("1.0");
        if (type == AccountTypeEnum.debit) {
            obj.setDebit(String.valueOf(money));
        } else if (type == AccountTypeEnum.credit) {
            obj.setCredit(String.valueOf(money));
        } else {
            throw new BusinessException("借贷类型不正确。");
        }
        obj.setDetailId(dim);
        this.entity.add(obj);
    }
}
